<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnClose">
    <!-- 表单部分 -->
    <el-form
      ref="form"
      :model="formData"
      :rules="rulesForm"
      label-width="120px"
    >
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:70%"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:70%"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <!-- <el-input v-model="form.name" style="width:70%"></el-input> -->
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="formData.timeOfEntry"
          style="width: 70%;"
        ></el-date-picker>
      </el-form-item>
      <!--   :formatter="formOfEmployment" -->
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          placeholder="请选择"
          v-model="formData.formOfEmployment"
          style="width:70%"
        >
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:70%"></el-input>
      </el-form-item>
      <el-form-item v-loading="loading" label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width:70%"
          @focus="getDepartments"
        ></el-input>
        <!-- 放置一个树形组件 -->
        <el-tree
          @node-click="selectNode"
          v-if="showTree"
          :data="treeList"
          :props="{ label: 'name' }"
          default-expand-all
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间">
        <!-- <el-input v-model="form.name" style="width:70%"></el-input> -->
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="formData.correctionTime"
          style="width: 70%;"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <!-- 底部区域 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="btnClose">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 获取组件机构API接口
import { getDepartments } from "@/api/departments";
// 导入组件架构组件
import { transListDateTree } from "@/utils/index";
// 新增员工接口
import { addEmployee } from "@/api/employees";
// 引入员工数据状态
import EmployeeEnum from "@/api/constant/employees";
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum, // 引入它并赋值(同类名),定义表单数据
      showTree: false,
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: ""
      },
      treeList: [], // 接受树形结构数据
      loading: false,
      rulesForm: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          { min: 1, max: 6, message: "用户名为1-6位", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}/,
            message: "手机号格式不正确",
            trigger: "blur"
          }
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "change" }
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" }
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" }
        ],
        timeOfEntry: [
          { required: true, message: "入职时间不能为空", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    // 获取部门聚焦的时候，调用组织架构Api接口
    async getDepartments() {
      try {
        this.showTree = true;
        this.loading = true;
        const { depts } = await getDepartments();
        console.log(depts);
        this.treeList = transListDateTree(depts, "");
        this.loading = false;
      } catch (error) {
        console.log(error);
      }
    },
    selectNode(node) {
      // 打印出当前需要传入的参数，所对应的内容
      // console.log(arguments);
      this.formData.departmentName = node.name;
      this.showTree = false;
    },
    // // 格式化聘用形式
    // formOfEmployment(row, column, cellValue, index) {
    //   console.log(arguments);
    //   const obj = EmployeeEnum.hireType.find(item => item.id === cellValue);
    //   return obj ? obj.value : "未知";
    // },
    // 点击确认，调用新增员工接口
    async btnOK() {
      try {
        await this.$refs.form.validate();
        const res = await addEmployee(this.formData);
        // console.log(res);
        // this.$parent 可以直接调用到父组件的实例 实际上就是父组件this
        // 渲染页面，通知父组件，跟新数据
        // debugger;
        // console.log(this.$parent);
        this.$parent.getEmployeesList();
        this.$parent.showDialog = false;
        this.$message.success("添加员工成功");
      } catch (error) {
        console.log(error);
      }
    },
    // 点击取消
    btnClose() {
      // 清空表单的内容
      (this.formData = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: ""
      }),
        this.$refs.form.resetFields();
      // 向父组件传值，关闭弹层
      // update 跟 sync 连用
      this.$emit("update:showDialog", false);
    }
  }
};
</script>

<style scoped></style>
